package cn.pping.mq.consumer;

import cn.pping.domain.PayOrder;
import cn.pping.dto.Order2PayOrderParamDto;
import cn.pping.service.IPayOrderService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup = "service-pay-consumer",//消费者组名
        topic = "topic-order",//topic
        selectorExpression = "tag-order",//tag
        messageModel = MessageModel.CLUSTERING //集群
)
public class PayOrderConsumer  implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        if (messageExt.getBody() == null) {
            return;
        }
        String s = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        //消费业务
        Order2PayOrderParamDto orderDto = JSONObject.parseObject(s, Order2PayOrderParamDto.class);
        Wrapper<PayOrder> wrapper = new EntityWrapper<>();
        wrapper.eq("order_no",orderDto.getOrderNo());
        PayOrder payOrder = payOrderService.selectOne(wrapper);
        if(payOrder != null){
            return;//自动签收，以后不要再发给我了 说明已经好了
        }
        payOrderService.savePayOrder(orderDto);


    }
}
